#!/usr/bin/perl 
#
# Quick photometry with munipack
#
# $Id$

print "QMPhot - 0.2    Quick Photometry with Munipack\n";

$state = " ";
while ($arg = shift(@ARGV)) {
#    print "$arg\n";
    if( $arg eq "-d" ) {
        $state = "-d";
    } elsif( $arg eq "-f" ) {
	$state = "-f";
    } elsif( $arg eq "-i" ) {
	$state = "-i";
    } elsif( $arg eq "-h" ) {
	&help();
	exit 0;
    } else {
	if( $state eq "-d" ) { 
#	    print "dark: $arg\n"; 
	    $dark = "$dark $arg";
	} elsif( $state eq "-f" ) { 
#	    print "flat: $arg\n"; 
	    $flat = "$flat $arg";
	} elsif( $state eq "-i" ) { 
#	    print "image: $arg\n"; 
	    $image = "$image $arg";
	} else { 
	    print "Try -h for more info.\n"; 
	    exit 0; 
	}
    }
} 
if( $state eq " " ) {
    print "Try -h for more info.\n"; 
    exit 0; 
}


#print "\n$dark $flat $image\n";

open(SH,">qmphot.sh");

# print header
print SH "#!/bin/sh -x\n";
print SH "#\n";
print SH "# qmphot.sh - generaded by qmphot.pl\n\n";

print SH "# convert ST darks to FITS:\n";
$ndark = 0;
$a = "";
foreach $i (split(" ",$dark)) {
    $ndark = $ndark + 1;
    $_ = $i;
    if( /.fits/i ) {
	$namedark = $i; }
    else {
        $namedark = "d$ndark.fits"; }
    $a = "$a\n$namedark";
    if( ! /.fits/i ) {     
        print SH "nice konve $i mask=$namedark\n"; }
}
if( $ndark > 1 ) {
    print SH "# mean of darks\n";
    print SH "echo -e \"$a\" | nice mdark\n";
    print SH "mv meandark.fits dark.fits\n";
} elsif( $ndark == 1 ) {
    print SH "mv $namedark dark.fits\n";
}

print SH "# convert ST flats to FITS:\n";
$nflat = 0;
$a = "";
foreach $i (split(" ",$flat)) {
    $nflat = $nflat + 1;
    $_ = $i;
    if( /.fits/i ) {
	$nameflat = $i; }
    else {
        $nameflat = "f$nflat.fits"; }	
    $a = "$a\n$nameflat";
    if( ! /.fits/i ) { 
	print SH "nice konve $i mask=$nameflat\n";}
}
if( $nflat > 1 ) {
    print SH "# autoflat\n";
    print SH "echo -e \"$a\" | nice aflat\n";
    print SH "mv autoflat.fits flat.fits\n";
} elsif( $nflat == 1 ) {
    print SH "mv $nameflat flat.fits\n";
}

print SH "# convert scientific images to FITS\n";
open(IMG,">image.lst");
$nimage = 0;
foreach $i (split(" ",$image)) {
    $nimage = $nimage + 1;
    $_ = $i;
    if( /.fits/i ) {
	$name = $i; }
    else {
	$name = "image$nimage.fits";
	print SH "nice konve $i mask=$name\n"; }
    print IMG "$name\n";
}
close IMG;

if( $ndark > 0 && $nimage > 0 ) {
    print SH "# subtracting of darks\n";
    print SH "nice darkbat \@image.lst dark=dark.fits mask=.\n";
}

if( $nflat > 0 && $nimage > 0 ) {
    print SH "# correcting by flat\n";
    print SH "nice flatbat \@image.lst flat=flat.fits mask=.\n";
}

if( $nimage > 0 ) {

    open(MPHOT,">mphot.opt");
    print MPHOT "READ NOISE (ADU; 1 frame)  = 15\n";
    print MPHOT "GAIN (e-/ADU; 1 frame)     = 2.3\n";
    print MPHOT "LOW GOOD DATUM (in sigmas) = 7\n";
    print MPHOT "HIGH GOOD DATUM (in ADU)   = 65000.0\n";
    print MPHOT "FWHM OF OBJECT             = 2.0\n";
    print MPHOT "THRESHOLD (in sigmas)      = 5.0\n";
    print MPHOT "LS (LOW SHARPNESS CUTOFF)  = 0.2\n";
    print MPHOT "HS (HIGH SHARPNESS CUTOFF) = 1.0\n";
    print MPHOT "LR (LOW ROUNDNESS CUTOFF)  = -1.0\n";
    print MPHOT "HR (HIGH ROUNDNESS CUTOFF) = 1.0\n";
    print MPHOT "WATCH PROGRESS             = 0.0\n";
    print MPHOT "FITTING RADIUS             = 2.0\n";
    print MPHOT "PSF RADIUS                 = 15.0\n";
    print MPHOT "VARIABLE PSF               = 0.0\n";
    print MPHOT "FRACTIONAL-PIXEL EXPANSION = 0.0\n";
    print MPHOT "ANALYTIC MODEL PSF         = 1\n";
    print MPHOT "EXTRA PSF CLEANING PASSES  = 5\n";
    print MPHOT "PERCENT ERROR (in %)       = 0.75\n";
    print MPHOT "PROFILE ERROR (in %)       = 5\n";
    close MPHOT;

    open(MPHOTO,">mphoto.opt");
    print MPHOTO "A1  RADIUS OF APERTURE  1  = 2.0\n";
    print MPHOTO "A2  RADIUS OF APERTURE  2  = 2.7273\n";
    print MPHOTO "A3  RADIUS OF APERTURE  3  = 3.8182\n";
    print MPHOTO "A4  RADIUS OF APERTURE  4  = 5.2727\n";
    print MPHOTO "A5  RADIUS OF APERTURE  5  = 7.0909\n";
    print MPHOTO "A6  RADIUS OF APERTURE  6  = 9.2727\n";
    print MPHOTO "A7  RADIUS OF APERTURE  7  = 11.8182\n";
    print MPHOTO "A8  RADIUS OF APERTURE  8  = 14.7273\n";
    print MPHOTO "A9  RADIUS OF APERTURE  9  = 18.0\n";
    print MPHOTO "AA  RADIUS OF APERTURE 10  = 21.6364\n";
    print MPHOTO "AB  RADIUS OF APERTURE 11  = 25.6364\n";
    print MPHOTO "AC  RADIUS OF APERTURE 12  = 30.0\n";
    print MPHOTO "IS       INNER SKY RADIUS  = 20.0\n";
    print MPHOTO "OS       OUTER SKY RADIUS  = 30.0\n";
    close MPHOTO;

    open(COM,">com");
    print COM "attach\n";
    print COM "sky\n";
    print COM "find 1 1\n";
    print COM "photometry\n";
    print COM "sort AP n +4\n";
    close COM;

    print SH "# photometry\n";
    print SH "nice muniphot \@image.lst com=com\n";
    
    open(MATCH,">match.opt");
    print MATCH "Maximum read stars        = 10\n";
    print MATCH "Max. identification stars = 5\n";
    print MATCH "Sigma clipping factor     = 2.5\n";
    print MATCH "Watch progress            = 0\n";
    print MATCH "Blank lines               = 1\n";
    close MATCH;

    print SH "# matching\n";
    print SH "ls *.SRT | nice munimatch \@ ref=`head -1 image.lst | sed s/.fits/.SRT/`\n";

    close SH;
}

system 'chmod u+x qmphot.sh'; 

print "\n";
print "The photometry script was created. You can edit its or some config files.\n";
print "Please run 'qmphot' to photometry of your data.\n";
print "Use '(nice qmphot.sh >& qmphot.log &)' for switch processing to background.\n";
print "\n";

exit;

sub help {
    print "Usage: qmplot.sh [-h] [-d <dark(s)>] [-f <flat(s)>] [-i <image(s)>]\n";
    print "\n";
    print "  This script help you to create simple script to photometry\n";
    print "  and it will do:\n";
    print "  convert ST files to FITS, create mean dark and flat,\n";
    print "  make fotometric corrections of scientific images,\n";
    print "  photometry and matching.\n";
    print "\n";
    }

